import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置NProgress
NProgress.configure({ showSpinner: false })

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    component: () => import('@/layouts/BasicLayout.vue'),
    redirect: '/dashboard',
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '仪表板',
          icon: 'Dashboard'
        }
      },
      {
        path: 'questions',
        name: 'Questions',
        component: () => import('@/views/questions/index.vue'),
        meta: {
          title: '题目管理',
          icon: 'Document'
        }
      },
      {
        path: 'questions/create',
        name: 'QuestionCreate',
        component: () => import('@/views/questions/create.vue'),
        meta: {
          title: '创建题目',
          hidden: true
        }
      },
      {
        path: 'questions/edit/:id',
        name: 'QuestionEdit',
        component: () => import('@/views/questions/edit.vue'),
        meta: {
          title: '编辑题目',
          hidden: true
        }
      },
      {
        path: 'papers',
        name: 'Papers',
        component: () => import('@/views/papers/index.vue'),
        meta: {
          title: '试卷管理',
          icon: 'Notebook'
        }
      },
      {
        path: 'papers/create',
        name: 'PaperCreate',
        component: () => import('@/views/papers/create.vue'),
        meta: {
          title: '创建试卷',
          hidden: true
        }
      },
      {
        path: 'papers/edit/:id',
        name: 'PaperEdit',
        component: () => import('@/views/papers/edit.vue'),
        meta: {
          title: '编辑试卷',
          hidden: true
        }
      },
      {
        path: 'students',
        name: 'Students',
        component: () => import('@/views/students/index.vue'),
        meta: {
          title: '学生管理',
          icon: 'User'
        }
      },
      {
        path: 'users',
        name: 'Users',
        component: () => import('@/views/users/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'UserFilled'
        }
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/views/statistics/index.vue'),
        meta: {
          title: '数据统计',
          icon: 'DataAnalysis'
        }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('@/views/settings/index.vue'),
        meta: {
          title: '系统设置',
          icon: 'Setting'
        }
      }
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在'
    }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  
  const userStore = useUserStore()
  
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 苒兴教育系统` : '苒兴教育系统'
  
  // 检查是否需要认证
  if (to.meta.requiresAuth !== false) {
    if (!userStore.isLoggedIn) {
      // 尝试从本地存储恢复登录状态
      await userStore.checkAuth()
      
      if (!userStore.isLoggedIn) {
        next('/login')
        return
      }
    }
  }
  
  // 如果已登录用户访问登录页，重定向到首页
  if (to.path === '/login' && userStore.isLoggedIn) {
    next('/')
    return
  }
  
  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router